package me.albert.entitylimiter.utils.inventory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import me.albert.entitylimiter.EntityLimiter;
import me.albert.entitylimiter.utils.ItemUtil;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

public class PagedInv {
    public static ConcurrentHashMap<UUID, ItemStack> trashes;
    public static ArrayList<Inventory> invs;

    public static void addItem(final ItemStack item) {
        for (final Inventory inventory : PagedInv.invs) {
            for (int i = 0; i < 45; ++i) {
                final ItemStack itemStack = inventory.getItem(i);
                if (itemStack == null || itemStack.getType().equals(Material.AIR)) {
                    inventory.addItem(item);
                    return;
                }
            }
        }
        final Inventory inventory2 = Bukkit.createInventory(new TiHolder(PagedInv.invs.size()), 54, "§8回收站 " + (PagedInv.invs.size() + 1));
        inventory2.setItem(45, ItemUtil.make(Material.ARROW, "§8上一页"));
        inventory2.setItem(53, ItemUtil.make(Material.ARROW, "§8下一页"));
        inventory2.addItem(item);
        PagedInv.invs.add(inventory2);
    }

    public static void updateInv() {
        HashMap<Player, Integer> tempInv = new HashMap<>();

        for(Player p : Bukkit.getOnlinePlayers()) {
            if(p.getOpenInventory().getTopInventory().getHolder() instanceof TiHolder) {
                TiHolder tiHolder = (TiHolder)p.getOpenInventory().getTopInventory().getHolder();
                tempInv.put(p, tiHolder.getPage());
                p.closeInventory();
            }
        }

        invs = new ArrayList<>();
        Bukkit.getScheduler().runTaskAsynchronously(EntityLimiter.getInstance(), () -> {
            if(trashes.size() > 1000) {
                trashes.clear();
            }

            ArrayList<ItemStack> items = new ArrayList<>(trashes.values());
            invs = getInvs(items, "§8回收站");
            Bukkit.getScheduler().runTask(EntityLimiter.getInstance(), () -> {
                for (Player p : tempInv.keySet()) {
                    int page = tempInv.get(p);
                    if(invs.size() > page) {
                        p.openInventory((Inventory)invs.get(page));
                    }
                }
            });
        });
    }

    public static ArrayList<Inventory> getInvs(final ArrayList<ItemStack> items, final String title) {
        final ArrayList<Inventory> invs = new ArrayList<Inventory>();
        final int size = (int) Math.ceil(items.size() / 45.0f);
        int t = 0;
        for (int i = 0; i < size; ++i) {
            final Inventory inv = Bukkit.createInventory(new TiHolder(i), 54, title + (i + 1) + "/" + size);
            for (int i2 = 0; i2 < 45 && t < items.size(); ++t, ++i2) {
                inv.addItem(items.get(t));
            }
            if (i != 0) {
                inv.setItem(45, ItemUtil.make(Material.ARROW, "§8上一页"));
            }
            if (i < size - 1) {
                inv.setItem(53, ItemUtil.make(Material.ARROW, "§8下一页"));
            }
            invs.add(inv);
        }
        return invs;
    }

    static {
        PagedInv.trashes = new ConcurrentHashMap<UUID, ItemStack>();
        PagedInv.invs = new ArrayList<Inventory>();
    }
}
